// created by noellynch
// Jun 3, 2009

package com.sceneworker.monitorpopup;

import java.awt.event.ActionEvent;
import java.util.concurrent.Callable;

import javax.swing.JDialog;
import javax.swing.tree.TreePath;

import com.jme.renderer.Renderer;
import com.jme.renderer.pass.Pass;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.BlendState.DestinationFunction;
import com.jme.scene.state.BlendState.SourceFunction;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.sceneworker.app.globals.SceneWorkerDialogManager;
import com.sceneworker.monitorpopup.panels.ImportDialogPanel;
import com.sceneworker.monitorpopup.panels.NewRenderStatePanel;

public class PassMenu extends BaseMenu {

	private static final String CLEAR_ALL_STATES = "Clear All States";
	private static final String PASTE = "Paste";
	private static final String ADD_NODE = "Add Node";
	private static final String ADD_FULL_SCREEN_QUAD = "Add Full Screen Quad";
	private static final String IMPORT_RENDER_STATE = "Import RenderState";
	private static final String NEW_RENDER_STATE = "New RenderState";
	
	private		Pass			mcl_pass;
	
	public PassMenu(TreePath cl_selectedPath, SceneWorkerPopUp cl_menu) {
		super(cl_menu);
		
		mcl_pass = (Pass)cl_selectedPath.getLastPathComponent();
		
		buildMenu();
	}

	@Override
	public void actionPerformed(ActionEvent cl_e) {
		if(cl_e.getActionCommand().equals(ADD_NODE)) {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					Node		lcl_node = new Node("node " + mcl_pass.size());
					mcl_pass.add(lcl_node);
					return null;
				}
			});
		}
		
		if(cl_e.getActionCommand().equals(NEW_RENDER_STATE)) {
			JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.NEW_RENDER_STATE_DIALOG);
			lcl_dlg.add(new NewRenderStatePanel(mcl_pass, lcl_dlg));
			lcl_dlg.setVisible(true);
			
			return;
		}
		
		if(cl_e.getActionCommand().equals(IMPORT_RENDER_STATE)) {
			JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.IMPORT_CHILD_DIALOG);
			lcl_dlg.add(new ImportDialogPanel(mcl_pass, lcl_dlg, true));
			lcl_dlg.setVisible(true);
			return;
		}
		
		if(cl_e.getActionCommand().equals(ADD_FULL_SCREEN_QUAD)) {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					// TODO : Dialog this
					DisplaySystem		lcl_display = DisplaySystem.getDisplaySystem();
					Quad		lcl_quad = new Quad("fullScreen" + mcl_pass.size(), lcl_display.getWidth(), lcl_display.getHeight());
					lcl_quad.getLocalRotation().set(0, 0, 0, 1);
					lcl_quad.getLocalTranslation().set(lcl_display.getWidth() / 2, lcl_display.getHeight() / 2, 0);
					lcl_quad.setLocalScale(1);
					lcl_quad.setRenderQueueMode(Renderer.QUEUE_ORTHO);
					lcl_quad.setCullHint(CullHint.Never);
					
					lcl_quad.updateRenderState();
					lcl_quad.updateGeometricState(0, true);
					
					mcl_pass.add(lcl_quad);
					return null;
				}
			});
		}
		
		if(cl_e.getActionCommand().equals(PASTE)) {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					if(mcl_menu.getObjectBuffer() instanceof Spatial) {
						mcl_pass.add((Spatial)mcl_menu.getObjectBuffer());
					} else if (mcl_menu.getObjectBuffer() instanceof RenderState) {
						mcl_pass.setPassState((RenderState)mcl_menu.getObjectBuffer());
					}
					return null;
				}
				
			});
		}
		
		if(cl_e.getActionCommand().equals(CLEAR_ALL_STATES)) {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					mcl_pass.clearPassStates();
					return null;
				}
				
			});
		}
	}

	@Override
	protected void buildMenu() {
		mcl_menu.add(buildAMenuItem(ADD_FULL_SCREEN_QUAD));
		mcl_menu.add(buildAMenuItem(ADD_NODE));
		mcl_menu.add(buildAMenuItem(NEW_RENDER_STATE));
		mcl_menu.add(buildAMenuItem(IMPORT_RENDER_STATE));
		mcl_menu.add(buildAMenuItem(CLEAR_ALL_STATES));
		
		if(mcl_menu.getObjectBuffer() != null) {
			if((mcl_menu.getObjectBuffer() instanceof Spatial) || (mcl_menu.getObjectBuffer() instanceof RenderState)) {
				mcl_menu.add(buildAMenuItem(PASTE));
			}
		}
	}

}
